import axios, { AxiosResponse } from 'axios';
import type { RouteRecordNormalized } from 'vue-router';
import { UserState } from '@/store/modules/user/types';
import type { HttpResponse } from './interceptor';

export interface CaptchaAxiosResponse<T = any>
  extends AxiosResponse<HttpResponse<T>> {
  img: string;
  uuid: string;
  [key: string]: any;
}

export interface LoginData {
  username: string;
  password: string;
  code: string;
  uuid: string;
}

export interface LoginRes {
  token: string;
}

export function captchaImage(): Promise<CaptchaAxiosResponse<HttpResponse>> {
  return axios.get('/api/captchaImage');
}

export function login(data: LoginData): Promise<LoginRes> {
  return axios.post('/api/login', data);
}

export function logout() {
  return axios.post<LoginRes>('/api/logout');
}

export function getUserInfo(): Promise<UserState> {
  return axios.get('/api/getInfo');
}

export function getMenuList() {
  return axios.get<RouteRecordNormalized[]>('/api/getRouters');
}
